<script setup>
  import { ref } from 'vue'
  import { Edit, Delete } from '@element-plus/icons-vue'
  import ChannelSelect from './components/ChannelSelect.vue'
  import { getArticleListService, delArticleService } from '@/api/article.js'
  import { formatTime } from '@/utils/format.js'
  import ArticleEdit from './components/ArticleEdit.vue'

  const articleList = ref([])
  const total = ref(0)
  const loading = ref(false)

  const params = ref({
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
  })
  // 获取文章列表
  const getArticleList = async () => {
    loading.value = true
    const res = await getArticleListService(params.value)
    articleList.value = res.data.data
    total.value = res.data.total
    loading.value = false
  }
  getArticleList()

  // 处理分页逻辑
  const onSizeChange = (size) => {
    // 当前页没意义
    params.value.pagenum = 1
    params.value.pagesize = size
    getArticleList()
  }
  const onCurrentChange = (page) => {
    params.value.pagenum = page
    getArticleList()
  }

  // 搜索
  const handleSearch = () => {
    params.value.pagenum = 1
    getArticleList()
  }
  // 重置
  const handleReset = () => {
    params.value.pagenum = 1
    params.value.cate_id = ''
    params.value.state = ''
    getArticleList()
  }
  const articleEditRef = ref()
  // 添加
  const handleAddArticle = () => {
    articleEditRef.value.open({})
  }
  // 删除
  const handleDeleteArticle = async (row) => {
    await ElMessageBox.confirm('你确定要删除该文章吗？', '温馨提示', {
      type: 'warning',
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    })
    await delArticleService(row.id)
    ElMessage({ type: 'success', message: '删除成功' })
    getArticleList()
  }
  // 编辑
  const handleEditArticle = (row) => {
    articleEditRef.value.open(row)
  }

  // 添加或编辑成功的回调
  const onSuccess = (type) => {
    if (type === 'add') {
      // 如果是添加,最好渲染最后一页
      const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
      params.value.pagenum = lastPage
    } else {
      // 如果是编辑,直接渲染当前页
    }
    getArticleList()
  }
</script>

<template>
  <PageContainer title="文章管理">
    <!-- 具名插槽 -->
    <template #extra>
      <el-button type="primary" @click="handleAddArticle">添加文章</el-button>
    </template>

    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类:">
        <!--
          Vue的v-model本质是语法糖，会被编译为：
          绑定modelValue属性
          监听update:modelValue事件
        -->
        <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select style="width: 240px" placeholder="请选择" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>

      <el-table-column label="文章分类" prop="cate_name"></el-table-column>
      <el-table-column label="发布时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"> </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="handleEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            type="danger"
            plain
            @click="handleDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 4, 5, 10]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <!-- 抽屉 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </PageContainer>
</template>

<style lang="scss" scoped></style>
